﻿using System;
using System.Collections;
using System.Text;

using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookClickToCallAddin
{
    public interface IClickToCallContacts
    {
        IClickToCallContact[] GetLocalContacts();
        void BuildContactLists(object application);
    }

    public interface IClickToCallContact
    {
        string ContactName { get; set; }
        string IMAddress { get; set; }
        string EmailAddress { get; set; }
        string Business2TelephoneNumber { get; set; }
        string BusinessTelephoneNumber { get; set; }
        string CallbackTelephoneNumber { get; set; }
        string CarTelephoneNumber { get; set; }
        string CompanyMainTelephoneNumber { get; set; }
        string HomeTelephoneNumber { get; set; }
        string MobileTelephoneNumber { get; set; }
        string OtherTelephoneNumber { get; set; }
        string PrimaryTelephoneNumber { get; set; } 
    }

    public class ClickToCallContacts : IClickToCallContacts
    {
        private IClickToCallContact[] localArray = null;


        #region IClickToCallContacts Members

        public void BuildContactLists(object application)
        {
            try
            {
                Outlook.Application outlookApp = (Outlook.ApplicationClass) application;
                Outlook.NameSpace mapi = outlookApp.GetNamespace("MAPI");
                
                Outlook.MAPIFolder contacts = mapi.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                IClickToCallContact newContact = null;
                localArray = new ClickToCallContact[contacts.Items.Count];
                for (int i = 1; i < contacts.Items.Count + 1; i++)
                {
                    Outlook.ContactItem c = (Outlook.ContactItem)contacts.Items[i];
                    newContact = new ClickToCallContact();
                    newContact.ContactName = c.FullName;
                    newContact.IMAddress = c.IMAddress;

                    if (c.Email1AddressType == "EX")
                    {
                        try
                        {
                            Outlook.AddressEntry ae = mapi.GetAddressEntryFromID(c.Email1EntryID);
                            newContact.EmailAddress = ae.Address;
                        }
                        catch
                        {
                            newContact.EmailAddress = c.Email1Address;
                        }
                    }
                    else
                    {
                        newContact.EmailAddress = c.Email1Address;
                    }
                    newContact.Business2TelephoneNumber = c.Business2TelephoneNumber;
                    newContact.BusinessTelephoneNumber = c.BusinessTelephoneNumber;
                    newContact.CallbackTelephoneNumber = c.CallbackTelephoneNumber;
                    newContact.CarTelephoneNumber = c.CarTelephoneNumber;
                    newContact.CompanyMainTelephoneNumber = c.CompanyMainTelephoneNumber;
                    newContact.HomeTelephoneNumber = c.HomeTelephoneNumber;
                    newContact.MobileTelephoneNumber = c.MobileTelephoneNumber;
                    newContact.OtherTelephoneNumber = c.OtherTelephoneNumber;
                    newContact.PrimaryTelephoneNumber = c.PrimaryTelephoneNumber;
                    localArray[i - 1] = newContact;
                }
            }
            catch
            {
            }
        }

        public IClickToCallContact[] GetLocalContacts()
        {
            return this.localArray;
        }

        #endregion
    }

    public class ClickToCallContact : IClickToCallContact
    {
        private string _contactName;
        public string ContactName
        {
            get { return _contactName; }
            set { _contactName = value; }
        }

        private string _imAddress;
        public string IMAddress
        {
            get { return _imAddress; }
            set { _imAddress = value; }
        }

        private string _emailAddress;
        public string EmailAddress
        {
            get { return _emailAddress; }
            set { _emailAddress = value; }
        }

        private string _business2TelephoneNumber;

        public string Business2TelephoneNumber
        {
            get { return _business2TelephoneNumber; }
            set { _business2TelephoneNumber = value; }
        }
        private string _businessTelephoneNumber;

        public string BusinessTelephoneNumber
        {
            get { return _businessTelephoneNumber; }
            set { _businessTelephoneNumber = value; }
        }
        private string _callbackTelephoneNumber;

        public string CallbackTelephoneNumber
        {
            get { return _callbackTelephoneNumber; }
            set { _callbackTelephoneNumber = value; }
        }
        private string _carTelephoneNumber;

        public string CarTelephoneNumber
        {
            get { return _carTelephoneNumber; }
            set { _carTelephoneNumber = value; }
        }
        private string _companyMainTelephoneNumber;

        public string CompanyMainTelephoneNumber
        {
            get { return _companyMainTelephoneNumber; }
            set { _companyMainTelephoneNumber = value; }
        }
        private string _homeTelephoneNumber;

        public string HomeTelephoneNumber
        {
            get { return _homeTelephoneNumber; }
            set { _homeTelephoneNumber = value; }
        }
        private string _mobileTelephoneNumber;

        public string MobileTelephoneNumber
        {
            get { return _mobileTelephoneNumber; }
            set { _mobileTelephoneNumber = value; }
        }
        private string _otherTelephoneNumber;

        public string OtherTelephoneNumber
        {
            get { return _otherTelephoneNumber; }
            set { _otherTelephoneNumber = value; }
        }
        private string _primaryTelephoneNumber;

        public string PrimaryTelephoneNumber
        {
            get { return _primaryTelephoneNumber; }
            set { _primaryTelephoneNumber = value; }
        }
    }
}
